﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using WebChatServer.Models;

namespace WebChatServer.Controllers
{
    [HttpHeader("Access-Control-Allow-Origin", "*")]
    [HttpHeader("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, OPTIONS")]

    public class FriendController : ApiController
    {
        //
        // GET: /Friend/

        IFriendRepository _repository = new FriendRepository();

        public void Options()
        {

        }

        public Friend Get(string id)
        {
            return _repository.Get(id);
        }

        public Friend Post(Friend friend)
        {
            //return _repository.Add(stu);
            friend = _repository.AddFriend(friend);
            //var response = Request.CreateResponse<Friend>(HttpStatusCode.Created, friend);

            //string uri = Url.Link("DefaultApi", new { id = friend.Id });
            //response.Headers.Location = new Uri(uri);

            return friend; ;
        }

        public string Delete(string id)
        {
            string[] split = Regex.Split(id, "id=");
            string idMember = null, idFriend = null;

            idMember = split[0];
            idFriend = split[1];

            //return "member: " + idMember + " ,id:" + idFriend;
            string groupName = _repository.DeleteFriend(idMember, idFriend);
            return groupName;
            //return _repository.DeleteFriend(idMember, idFriend);
            //return new HttpResponseMessage(HttpStatusCode.NoContent);
        }

    }
}
